﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000239 RID: 569
	public abstract class BaseSkeletonHandleView : BaseView
	{
		// Token: 0x1700016B RID: 363
		// (get) Token: 0x06000C2B RID: 3115
		public abstract string DataId { get; }

		// Token: 0x1700016C RID: 364
		// (get) Token: 0x06000C2C RID: 3116
		protected abstract string DATA_FILE_NAME { get; }

		// Token: 0x1700016D RID: 365
		// (get) Token: 0x06000C2D RID: 3117 RVA: 0x00063F45 File Offset: 0x00062145
		// (set) Token: 0x06000C2E RID: 3118 RVA: 0x00063F4D File Offset: 0x0006214D
		public List<string> Keys
		{
			get
			{
				return this._Keys;
			}
			private set
			{
				this._Keys = value;
			}
		}

		// Token: 0x06000C2F RID: 3119 RVA: 0x00063F56 File Offset: 0x00062156
		protected override void OnEnable()
		{
			base.OnEnable();
			this.UpdateKeys();
		}

		// Token: 0x06000C30 RID: 3120 RVA: 0x00063F64 File Offset: 0x00062164
		public void UpdateKeys()
		{
			CharacterEditor targetEditor = this.handleManager.TargetEditor;
			if (!targetEditor)
			{
				return;
			}
			if (!this.LoadKeys(targetEditor, this.DATA_FILE_NAME))
			{
				this.LoadDefaultKeys(targetEditor);
			}
		}

		// Token: 0x06000C31 RID: 3121 RVA: 0x00063FA4 File Offset: 0x000621A4
		public void SaveKeys(CharacterEditor editor)
		{
			string folderPath = editor.FolderPath;
			if (!Directory.Exists(folderPath))
			{
				Directory.CreateDirectory(folderPath);
			}
			string path = folderPath + this.DATA_FILE_NAME;
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			FileStream fileStream = File.Create(path);
			binaryFormatter.Serialize(fileStream, this.Keys.ToArray());
			fileStream.Close();
		}

		// Token: 0x06000C32 RID: 3122 RVA: 0x00063FFC File Offset: 0x000621FC
		private bool LoadKeys(CharacterEditor editor, string fileName)
		{
			string path = editor.FolderPath + fileName;
			if (File.Exists(path))
			{
				try
				{
					BinaryFormatter binaryFormatter = new BinaryFormatter();
					FileStream fileStream = File.Open(path, FileMode.Open);
					this.Keys = new List<string>((string[])binaryFormatter.Deserialize(fileStream));
					fileStream.Close();
					return true;
				}
				catch
				{
					return false;
				}
				return false;
			}
			return false;
		}

		// Token: 0x06000C33 RID: 3123
		protected abstract void LoadDefaultKeys(CharacterEditor targetEditor);

		// Token: 0x04001167 RID: 4455
		private List<string> _Keys = new List<string>();

		// Token: 0x04001168 RID: 4456
		[SerializeField]
		private HandleManager handleManager;
	}
}
